/*
 * Copyright 2012 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.coms514.scheduleme;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Activity to add or edit a calendar.
 * 
 * @author Yaniv Inbar
 */
public class AddOrEditCaregiverActivity extends Activity {

	static final String TAG = "AddOrEditCaregiverActivity";
	
	private String id;
	private String name;
	private String email;
	private String phone;
	
	private EditText nameEditText;
	private EditText emailEditText;
	private EditText phoneEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.addcaregiver);
		nameEditText = (EditText) findViewById(R.id.name);
		emailEditText = (EditText) findViewById(R.id.email);
		phoneEditText = (EditText) findViewById(R.id.phone);
		TextView titleTextView = (TextView) findViewById(R.id.textViewTitle);
		
		id = getIntent().getStringExtra("id");
		if (id != null) {
			name = getIntent().getStringExtra("name");
			email = getIntent().getStringExtra("email");
			phone = getIntent().getStringExtra("phone");

			titleTextView.setText(R.string.edit_caregiver);
			nameEditText.setText(name);
			emailEditText.setText(email);
			phoneEditText.setText(phone);
		} else {
			titleTextView.setText(R.string.add_caregiver);
		}
	}	

	public void onSave(View view) {
		name = nameEditText.getText().toString();
		email = emailEditText.getText().toString();
		phone = phoneEditText.getText().toString();
		
		if (name.length() > 0 && email.length() > 0 && phone.length() > 0) {
			Intent intent = new Intent();
			intent.putExtra("id", id);
			intent.putExtra("name", name);
			intent.putExtra("email", email);
			intent.putExtra("phone", phone);
			setResult(Activity.RESULT_OK, intent);
		} else {
			setResult(Activity.RESULT_CANCELED);
		}
		finish();
	}

	public void onCancel(View view) {
		setResult(Activity.RESULT_CANCELED);
		finish();
	}
	
	/* for logging */
	@SuppressWarnings("unused")
	private void println(String msg) {
		Log.d(TAG, msg);
	}
}
